﻿using System;
class MatrixMaxSum
{
    static void Main()
    {
        int matrixSizeRow;
        int matrixSizeVal;
        int sum = 0;
        int bestSum = 0;
        Console.Write("please enter rows of matrix: ");
        while (true)
        {
            try
            {
                matrixSizeRow = int.Parse(Console.ReadLine());
                break;
            }
            catch (Exception)
            {
                Console.Write("Please re-enter size:");
            }
        }
        Console.Write("please enter vals of matrix: ");
        while (true)
        {
            try
            {
                matrixSizeVal = int.Parse(Console.ReadLine());
                break;
            }
            catch (Exception)
            {
                Console.Write("Please re-enter size:");
            }
        }
        Console.WriteLine();
        int[,] theArray = new int[matrixSizeRow, matrixSizeVal];
        int[,] bestElems = new int[3, 3];
        Random rnd = new Random();
        int counter = 1;
        for (int i = 0; i < matrixSizeRow; i++)
        {
            for (int j = 0; j < matrixSizeVal; j++)
            {
                theArray[i, j] = rnd.Next(1, 21);
            }
        }
        for (int i = 0; i < matrixSizeRow; i++)
        {
            for (int j = 0; j < matrixSizeVal; j++)
            {
                if (j == matrixSizeVal - 1)
                {
                    Console.WriteLine("{0,-2} ", theArray[i, j]);
                }
                else
                {
                    Console.Write("{0,-2} ", theArray[i, j]);
                }
            }
        }
        for (int i = 0; i < matrixSizeRow - 2; i++)
        {
            for (int j = 0; j < matrixSizeVal - 2; j++)
            {
                sum = theArray[i, j] + theArray[i + 1, j] + theArray[i + 2, j]
                    + theArray[i, j + 1] + theArray[i + 1, j + 1] + theArray[i + 2, j + 1]
                    + theArray[i, j + 2] + theArray[i + 1, j + 2] + theArray[i + 2, j + 2];
                if (sum > bestSum)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        for (int l = 0; l < 3; l++)
                        {
                            bestElems[k, l] = theArray[i + k, j + l];
                        }
                    }
                    bestSum = sum;
                }
            }
        }
        Console.WriteLine();
        Console.WriteLine("the best sum is: {0}", bestSum);
        Console.WriteLine();
        Console.WriteLine("from elements:");
        Console.WriteLine();
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (j == 3 - 1)
                {
                    Console.WriteLine("{0,-2} ", bestElems[i, j]);
                }
                else
                {
                    Console.Write("{0,-2} ", bestElems[i, j]);
                }
            }
        }
        Console.ReadLine();
    }
}

